// pages/home/home.js
const http = require('../../utils/http.js')
Page({
  data: {
    // 轮播图地址
    imgUrl: [
      '../asset/image/lunbotu/banner1.jpg',
      '../asset/image/lunbotu/banner2.jpg',
      '../asset/image/lunbotu/banner3.jpg',
      '../asset/image/lunbotu/banner4.jpg',
    ],
    // 酒品精选
    foursWine: [],
    // 图片地址前置
    baseImg: 'https://dianjiu.oss-cn-beijing.aliyuncs.com',
  },
  // 添加到购物车
  addToCart(e) {
    let info = e.currentTarget.dataset.goodsinfo
    var appInst = getApp()
    let carGoodsArr = appInst.globalData.cartGoods
    let bool = carGoodsArr.some((i) => {
      return i.commodity_id === info.commodity_id
    })
    if (bool) return
    info.num = 1
    info.checked = false
    appInst.globalData.cartGoods.push(info)
    try {
      wx.setStorageSync('goodsInfo', appInst.globalData.cartGoods)
    } catch (e) {}
  },
  // 酒品分类与更多跳转
  toClassification(e) {
    wx.showLoading({
      title: '加载中...',
    })
    setTimeout(function () {
      wx.hideLoading()
    }, 2000)
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/wineList/wineList',
      events: {
        acceptDataFromOpenedPage: function (data) {},
      },
      success: function (res) {
        // 通过 eventChannel 向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', { id })
      },
    })
  },
  // 商品详情跳转
  toDetails(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/details/details',
      events: {
        acceptDataFromOpenedPage: function (data) {},
      },
      success: function (res) {
        // 通过 eventChannel 向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', { id })
      },
    })
  },
  async onLoad() {
    try {
      var value = wx.getStorageSync('goodsInfo')
      if (value) {
        // Do something with return value
        var appInst = getApp()
        appInst.globalData.cartGoods = value
      }
    } catch (e) {
      // Do something when catch error
    }
    let res = await http({
      url: '/getFoursWine',
    })
    this.setData({
      foursWine: res,
    })
  },
  onReady() {},
  onShow() {},
  onHide() {},
  onUnload() {},
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
})
